<?php
/*
 * Copyright (c) 2011 Todd Brady
 *
 * PS3Theatre is freely distributable under the terms of the MIT license.
 * For details, see the MIT-Liscence web site: http://www.opensource.org/licenses/mit-license.php 
 */
 	include("includes/db_connect.php");
	
	$moveThe = "";
	$httpDir = "";
	$compDir = "";
	$wizard = $_GET['wizard'];
	
	$settingsQuery = "SELECT s.version, s.move_the FROM tbl_settings s";
	$cacheDirQuery = "SELECT d.computer_path, d.http_path FROM tbl_directories d, tbl_types t WHERE d.type_id=t.id AND t.type='Main'";
	$settings=mysql_query($settingsQuery) or die(mysql_error());
	$cache=mysql_query($cacheDirQuery) or die(mysql_error());
	$settingsRow = mysql_fetch_array($settings);
	$moveThe = $settingsRow['move_the'];
	if(mysql_num_rows($cache) > 0){
		$cacheDirRow = mysql_fetch_array($cache);
		$httpDir = $cacheDirRow['http_path'];
		$compDir = $cacheDirRow['computer_path'];
	}
	
	if(isset($_POST['httpDir']))
	{
		$wizard = $_POST['wizard'];
		$httpDir = $_POST['httpDir'];
		$compDir = $_POST['compDir'];
		if($_POST['moveThe'] == 'Y'){
			$moveThe = 'Y';
		}else{
			$moveThe = 'N';
		}
				
		$updateQuery = sprintf("update tbl_settings set move_the = '%s'",
			mysql_real_escape_string($moveThe));
		$deleteQuery = "delete from tbl_directories where type_id in (select id from tbl_types where type='Main')";
		$dirInsertQuery = sprintf("insert into tbl_directories (type_id,computer_path,http_path) select id, '%s', '%s' from tbl_types where type='Main'",
							mysql_real_escape_string($compDir),
							mysql_real_escape_string($httpDir));
			
			mysql_query($updateQuery) or die('error while trying to update: '.mysql_error());
			mysql_query($deleteQuery) or die('error while trying to delete dir row: '.mysql_error());
			mysql_query($dirInsertQuery) or die('error while trying to insert dir row: '.mysql_error());
			
			//update session vars
			$_SESSION['moveThe'] = $moveThe;
			$_SESSION['cacheHttpDir'] = $httpDir;
			$_SESSION['cacheCompDir'] = $compDir;
			
			$message = "Main settings successfully updated.";
		if($wizard == 'true'){
			//redirect to next step
			header("Location: weather-edit.php?wizard=true");
		}else{
			//redirect to main setup with message
			header("Location: main-setup.php?message=".$message);
		}
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<LINK href="../css/main.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="../js/jQuery.js"></script>
<script type="text/javascript" src="../js/simpleModal.js"></script>
</head>
<body>
<h4>Edit Main Settings</h4>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
<input name="wizard" type="hidden" value="<?php echo $wizard;?>" />
<table cellpadding="2" class="rowmajor">
 <tbody>
   <tr>
     <th class="vt"><div align="left">
       <p>Move 'the':</p>
       </div></th>
     <td><label>
       <input name="moveThe" type="checkbox" id="moveThe" value="Y" <?php if($moveThe=='Y'){echo 'checked="checked"';}?>/>
       </label></td>
     <td>Check this if you want to move the word 'the' to the end of titles e.g. &quot;Dark Knight, The&quot; </td>
   </tr>
   <tr>
     <th class="vt"><div align="left">Cache http dir :</div></th>
     <td><input name="httpDir" type="text" id="httpDir" size="50" maxlength="512" value="<?php echo $httpDir;?>"/></td>
     <td>Directory you want to cache images in (Must be a shared directory) <br/>example: <b>http://192.168.0.1/cache/</b> </td>
   </tr>
   <tr>
     <th class="vt"><div align="left">Cache computer dir :</div></th>
     <td><label>
     <input name="compDir" type="text" id="compDir" size="50" maxlength="512" value="<?php echo $compDir;?>"/>
     </label></td>
     <td>Directory you want to cache images in (Must be a shared directory) <br/>example: <b>//COMPUTERNAME/cache/</b></td>
   </tr>
 </tbody></table>
<p>
  <?php
  	if($wizard == 'true'){
  ?>
  <input type="button" name="cancelButton" value="Back" onclick="location='setup-main.php'"/>
  <input type="submit" name="Submit" value="Next" />
  <?php 
  }else{
  ?>
  <input type="submit" name="Submit" value="Save" />
  <input type="button" name="cancelButton" value="Cancel" onclick="location='main-setup.php'"/>
  <?php
  }
  ?>
</p>
</form>
<!-- Modal Window Code -->
<div id='confirm'>
	<div class='header'><span>default title</span></div>
	<div id="message" class='message'> </div>
</div>
<!-- preload the images -->
<div style='display:none'>
	<img src='../images/header.gif' alt='' />
</div>
</body>
</html>
